
Partial Class BlockingE
    Inherits System.Web.UI.Page

    Private Const Q As String = Chr(34)

    Private moServer As SQLAutoDocLib.Server = Nothing
    Private moDatabase As SQLAutoDocLib.Database = Nothing

    Private msDBID As String

    Private moChart As jChart.GraphChart
    Private moData As DataTable = Nothing

    Private Enum ViewMode As Integer
        SessionDetail = 1
        DayView = 2
        WeekView = 3
    End Enum

    Private mlMode As ViewMode

    Private msWeek As String
    Private msDay As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        msDBID = Request.QueryString("D")
        msWeek = Request.QueryString("W")
        msDay = Request.QueryString("DT")

        moDatabase = New SQLAutoDocLib.Database(msDBID)
        moDatabase.Load()

        moServer = New SQLAutoDocLib.Server(moDatabase.ServerID)
        moServer.Load()

        Dim oFactory As New SQLAutoDocLib.BlockLog_Factory

        If IsDate(msWeek) Then
            mlMode = ViewMode.DayView
            moData = oFactory.AverageDailyBlockingCounts(msWeek, msDBID)
            MakeDailyDatabaseBlockingChart()
            divZoom.Visible = True
            AZoom.HRef = "SQBlockingE.aspx?S=" & moDatabase.ServerID & "&D=" & moDatabase.ID
            ARefresh.HRef = "SQBlockingE.aspx?D=" & moDatabase.ID & "&W=" & HttpUtility.UrlEncode(msWeek)
            tdCaption.InnerText = "Average Blocks - Week"

        ElseIf IsDate(msDay) Then
            mlMode = ViewMode.SessionDetail
            moData = oFactory.AverageDayBlockingCounts(msDay, msDBID)
            MakeDayDatabaseBlockingChart()
            divZoom.Visible = True
            AZoom.HRef = "SQBlockingE.aspx?D=" & moDatabase.ID & "&W=" & CDate(msDay).AddDays(CDate(msDay).DayOfWeek - 6)
            ARefresh.HRef = "SQBlockingE.aspx?D=" & moDatabase.ID & "&DT=" & HttpUtility.UrlEncode(msDay)
            tdCaption.InnerText = "Average Blocks - Day"

        Else
            mlMode = ViewMode.WeekView
            moData = oFactory.AverageWeeklyBlockingCounts(12, msDBID)
            MakeDatabaseBlockingChart()
            divZoom.Visible = False

            ARefresh.HRef = "SQBlockingE.aspx?S=" & moDatabase.ServerID & "&D=" & moDatabase.ID
        End If
    End Sub

    Private Sub MakeDatabaseBlockingChart()
        '---Let's set up the chart.
        '   We'll make it 600 pixels wide by 400 pixels high.        
        moChart = New jChart.GraphChart(510, 460)
        moChart.ChartName = "Average Block Count - Last 12 weeks"

        '---We'll specify a 10 pixel border all the way around the inside
        '   edge of the chart.
        moChart.ChartDimensions.FrameSize = 10

        '---The Y Axis column on the left will be 20 pixels wide.
        moChart.ChartDimensions.YAxis1LabelColumnWidth = 20

        '---The X Axis row on the bottom will be 20 pixels high.
        moChart.ChartDimensions.XAxisLabelRowHeight = 20

        '---Let's add the X Axis values
        '   We'll have 7 values and in place of the numeric values we'll
        '   display friendly labels.
        Dim lCount As Integer = moData.Rows.Count
        Dim lMax As Long = 0

        Dim oXAxisValues(lCount - 1) As Single
        Dim oXAxisLabels(lCount - 1) As String
        Dim oSeries1Values(lCount - 1) As Single

        For i As Integer = 0 To lCount - 1
            oXAxisValues(i) = i
            oXAxisLabels(i) = moData.Rows(i).Item("dtWeekDate")

            If IsNumeric(moData.Rows(i).Item("CountOfBlocks")) Then
                oSeries1Values(i) = moData.Rows(i).Item("CountOfBlocks")

                If moData.Rows(i).Item("CountOfBlocks") > lMax Then
                    lMax = moData.Rows(i).Item("CountOfBlocks")
                End If
            Else
                oSeries1Values(i) = -999
            End If
        Next

        '---We'll create an XAxis object using the ChartDimensions object
        '   from the chart we just created.
        Dim oXAxis As New jChart.XAxis(moChart.ChartDimensions, oXAxisValues)

        '---Let's tell the X Axis about the friendly labels to display in place
        '   of numbers.
        oXAxis.XAxisLabels = oXAxisLabels
        oXAxis.FontAngle = jChart.XAxis.Rotation.Diagonal

        '---OK, finally let's tell the chart about its X Axis.
        moChart.XAxis = oXAxis

        '---Now, let's set up the Y Axis.
        Dim oYAxis1 As New jChart.YAxis(jChart.YAxis.Alignment.Left, moChart.ChartDimensions, 5, 0, lMax)

        Dim oYLabels(5) As String
        For i As Integer = 0 To 4

        Next

        '---Now let's tell the chart about our Y Axis 
        moChart.YAxis1 = oYAxis1

        '---Now that we have values, we'll need a series
        '   Our series will be a bar series.

        Dim oBarSeries1 As New jChart.Overlay.Series.BarSeries
        With oBarSeries1
            '---We will call our series "series one". 
            '   This value is accessible on drill-down from the image map.
            .Name = "Average Blocks"

            '---Now we'll give the series the values it should use.
            .Values = oSeries1Values

            '---The series will be green in color
            .Color = Color.Green

            '---The series will be slightly transparent (75% opaque)
            .Opacity = 0.75
        End With

        '---Since we are using a line chart, which belongs to the 
        '   LineOverlay overlay type, we'll need a LineOverlay object.
        Dim oBarOverlay As jChart.Overlay.BarOverlay

        '---We need to tell the overlay which dimensions and axes to use when
        '   we create it.
        oBarOverlay = New jChart.Overlay.BarOverlay(moChart.ChartDimensions, oXAxis, oYAxis1)

        '---Let's add the series to the overlay
        oBarOverlay.Series.Add(oBarSeries1)

        '---Let's add the overlay to the chart.
        moChart.OverLays.Add(oBarOverlay)

        '---OK. All done. Now let's draw the chart.
        '   We'll start by creating a bitmap onto which the chart will be drawn.
        '   We do it this way so that the bitmap can be shared (a background image
        '   perhaps?)
        Dim oPicture As New Bitmap( _
                moChart.ChartDimensions.OuterWidth, _
                moChart.ChartDimensions.OuterHeight, _
                System.Drawing.Imaging.PixelFormat.Format24bppRgb)

        '---Now that we've created the bitmap, we'll draw the chart on it and save it
        '   to a temporary location on the server in JPEG format. The charting object
        '   creates a unique filename for our new chart. This unique filename is
        '   accessible via the chart's FileName property (see ImageURL)

        '---The path where the image will be saved must have write access for the ASPNET
        '   process enabled.
        moChart.SaveChartToFile( _
                    Application("TempPath"), _
                    System.Drawing.Imaging.ImageFormat.Jpeg)
    End Sub

    Private Sub MakeDayDatabaseBlockingChart()
        '---Let's set up the chart.
        '   We'll make it 600 pixels wide by 400 pixels high.        
        moChart = New jChart.GraphChart(510, 460)
        moChart.ChartName = "Average Blocks - Day " & msDay

        '---We'll specify a 10 pixel border all the way around the inside
        '   edge of the chart.
        moChart.ChartDimensions.FrameSize = 10

        '---The Y Axis column on the left will be 20 pixels wide.
        moChart.ChartDimensions.YAxis1LabelColumnWidth = 20

        '---The X Axis row on the bottom will be 20 pixels high.
        moChart.ChartDimensions.XAxisLabelRowHeight = 20

        '---Let's add the X Axis values
        '   We'll have 7 values and in place of the numeric values we'll
        '   display friendly labels.
        Dim lCount As Integer = moData.Rows.Count
        Dim lMax As Long = 0

        Dim oXAxisValues(lCount - 1) As Single
        Dim oXAxisLabels(lCount - 1) As String
        Dim oSeries1Values(lCount - 1) As Single

        For i As Integer = 0 To lCount - 1
            oXAxisValues(i) = i
            oXAxisLabels(i) = CDate(moData.Rows(i).Item("dtWeekDate")).TimeOfDay.ToString.Substring(0, 5)

            If IsNumeric(moData.Rows(i).Item("AvgCountOfAllBlocks")) Then
                oSeries1Values(i) = moData.Rows(i).Item("AvgCountOfAllBlocks")

                If moData.Rows(i).Item("AvgCountOfAllBlocks") > lMax Then
                    lMax = moData.Rows(i).Item("AvgCountOfAllBlocks")
                End If
            Else
                oSeries1Values(i) = -999
            End If
        Next

        '---We'll create an XAxis object using the ChartDimensions object
        '   from the chart we just created.
        Dim oXAxis As New jChart.XAxis(moChart.ChartDimensions, oXAxisValues)

        '---Let's tell the X Axis about the friendly labels to display in place
        '   of numbers.
        oXAxis.XAxisLabels = oXAxisLabels
        oXAxis.FontAngle = jChart.XAxis.Rotation.Diagonal

        '---OK, finally let's tell the chart about its X Axis.
        moChart.XAxis = oXAxis

        '---Now, let's set up the Y Axis.
        Dim oYAxis1 As New jChart.YAxis(jChart.YAxis.Alignment.Left, moChart.ChartDimensions, 5, 0, lMax)

        Dim oYLabels(5) As String
        For i As Integer = 0 To 5
            oYLabels(i) = (lMax / 5) * i
        Next

        oYAxis1.YAxisLabels = oYLabels

        '---Now let's tell the chart about our Y Axis 
        moChart.YAxis1 = oYAxis1

        '---Now that we have values, we'll need a series
        '   Our series will be a bar series.

        Dim oBarSeries1 As New jChart.Overlay.Series.BarSeries
        With oBarSeries1
            '---We will call our series "series one". 
            '   This value is accessible on drill-down from the image map.
            .Name = "Average Blocks"

            '---Now we'll give the series the values it should use.
            .Values = oSeries1Values

            '---The series will be green in color
            .Color = Color.Green

            '---The series will be slightly transparent (75% opaque)
            .Opacity = 0.75
        End With

        '---Since we are using a line chart, which belongs to the 
        '   LineOverlay overlay type, we'll need a LineOverlay object.
        Dim oBarOverlay As jChart.Overlay.BarOverlay

        '---We need to tell the overlay which dimensions and axes to use when
        '   we create it.
        oBarOverlay = New jChart.Overlay.BarOverlay(moChart.ChartDimensions, oXAxis, oYAxis1)

        '---Let's add the series to the overlay
        oBarOverlay.Series.Add(oBarSeries1)

        '---Let's add the overlay to the chart.
        moChart.OverLays.Add(oBarOverlay)

        '---OK. All done. Now let's draw the chart.
        '   We'll start by creating a bitmap onto which the chart will be drawn.
        '   We do it this way so that the bitmap can be shared (a background image
        '   perhaps?)
        Dim oPicture As New Bitmap( _
                moChart.ChartDimensions.OuterWidth, _
                moChart.ChartDimensions.OuterHeight, _
                System.Drawing.Imaging.PixelFormat.Format24bppRgb)

        '---Now that we've created the bitmap, we'll draw the chart on it and save it
        '   to a temporary location on the server in JPEG format. The charting object
        '   creates a unique filename for our new chart. This unique filename is
        '   accessible via the chart's FileName property (see ImageURL)

        '---The path where the image will be saved must have write access for the ASPNET
        '   process enabled.
        moChart.SaveChartToFile( _
                    Application("TempPath"), _
                    System.Drawing.Imaging.ImageFormat.Jpeg)
    End Sub

    Private Sub MakeDailyDatabaseBlockingChart()
        '---Let's set up the chart.
        '   We'll make it 600 pixels wide by 400 pixels high.        
        moChart = New jChart.GraphChart(510, 460)
        moChart.ChartName = "Average Blocks - W/B " & msWeek

        '---We'll specify a 10 pixel border all the way around the inside
        '   edge of the chart.
        moChart.ChartDimensions.FrameSize = 10

        '---The Y Axis column on the left will be 20 pixels wide.
        moChart.ChartDimensions.YAxis1LabelColumnWidth = 20

        '---The X Axis row on the bottom will be 20 pixels high.
        moChart.ChartDimensions.XAxisLabelRowHeight = 20

        '---Let's add the X Axis values
        '   We'll have 7 values and in place of the numeric values we'll
        '   display friendly labels.
        Dim lCount As Integer = moData.Rows.Count
        Dim lMax As Long = 0

        Dim oXAxisValues(lCount - 1) As Single
        Dim oXAxisLabels(lCount - 1) As String
        Dim oSeries1Values(lCount - 1) As Single

        For i As Integer = 0 To lCount - 1
            oXAxisValues(i) = i
            oXAxisLabels(i) = moData.Rows(i).Item("dtWeekDate")

            If IsNumeric(moData.Rows(i).Item("AvgCountOfAllBlocks")) Then
                oSeries1Values(i) = moData.Rows(i).Item("AvgCountOfAllBlocks")

                If moData.Rows(i).Item("AvgCountOfAllBlocks") > lMax Then
                    lMax = moData.Rows(i).Item("AvgCountOfAllBlocks")
                End If
            Else
                oSeries1Values(i) = -999
            End If
        Next

        '---We'll create an XAxis object using the ChartDimensions object
        '   from the chart we just created.
        Dim oXAxis As New jChart.XAxis(moChart.ChartDimensions, oXAxisValues)

        '---Let's tell the X Axis about the friendly labels to display in place
        '   of numbers.
        oXAxis.XAxisLabels = oXAxisLabels
        oXAxis.FontAngle = jChart.XAxis.Rotation.Diagonal

        '---OK, finally let's tell the chart about its X Axis.
        moChart.XAxis = oXAxis

        '---Now, let's set up the Y Axis.
        Dim oYAxis1 As New jChart.YAxis(jChart.YAxis.Alignment.Left, moChart.ChartDimensions, 5, 0, lMax)

        Dim oYLabels(5) As String
        For i As Integer = 0 To 5
            oYLabels(i) = (lMax / 5) * i
        Next

        oYAxis1.YAxisLabels = oYLabels

        '---Now let's tell the chart about our Y Axis 
        moChart.YAxis1 = oYAxis1

        '---Now that we have values, we'll need a series
        '   Our series will be a bar series.

        Dim oBarSeries1 As New jChart.Overlay.Series.BarSeries
        With oBarSeries1
            '---We will call our series "series one". 
            '   This value is accessible on drill-down from the image map.
            .Name = "Average Blocks"

            '---Now we'll give the series the values it should use.
            .Values = oSeries1Values

            '---The series will be green in color
            .Color = Color.Green

            '---The series will be slightly transparent (75% opaque)
            .Opacity = 0.75
        End With

        '---Since we are using a line chart, which belongs to the 
        '   LineOverlay overlay type, we'll need a LineOverlay object.
        Dim oBarOverlay As jChart.Overlay.BarOverlay

        '---We need to tell the overlay which dimensions and axes to use when
        '   we create it.
        oBarOverlay = New jChart.Overlay.BarOverlay(moChart.ChartDimensions, oXAxis, oYAxis1)

        '---Let's add the series to the overlay
        oBarOverlay.Series.Add(oBarSeries1)

        '---Let's add the overlay to the chart.
        moChart.OverLays.Add(oBarOverlay)

        '---OK. All done. Now let's draw the chart.
        '   We'll start by creating a bitmap onto which the chart will be drawn.
        '   We do it this way so that the bitmap can be shared (a background image
        '   perhaps?)
        Dim oPicture As New Bitmap( _
                moChart.ChartDimensions.OuterWidth, _
                moChart.ChartDimensions.OuterHeight, _
                System.Drawing.Imaging.PixelFormat.Format24bppRgb)

        '---Now that we've created the bitmap, we'll draw the chart on it and save it
        '   to a temporary location on the server in JPEG format. The charting object
        '   creates a unique filename for our new chart. This unique filename is
        '   accessible via the chart's FileName property (see ImageURL)

        '---The path where the image will be saved must have write access for the ASPNET
        '   process enabled.
        moChart.SaveChartToFile( _
                    Application("TempPath"), _
                    System.Drawing.Imaging.ImageFormat.Jpeg)
    End Sub

    Protected Sub ShowImageTag()
        If Not moChart Is Nothing Then
            Dim sHTM As String = "<img src=" & Q & Session("TempURL") & moChart.FileName & Q & " alt=" & Q & Q & " usemap=" & Q & "#MyMap" & Q & " />"

            sHTM = sHTM & "<map id=" & Q & "Map1" & Q & " name=" & Q & "MyMap" & Q & ">"
            sHTM = sHTM & MakeImageMapString()
            sHTM = sHTM & "</map>"

            Response.Write(sHTM)
        End If
    End Sub

    Private Function MakeImageMapString() As String
        Dim sImageMap As String = ""

        '---The GetAllRegions function of the Chart object gets all regions
        '   of all series of all overlays, in order of display.
        For Each oRegion As jChart.RegionArea In moChart.RegionList
            Dim sCoords As String = ""

            '---The SeriesPoints property of the RegionArea object gives
            '   us the coordinates we need to describe a polygon around each
            '   series data point.
            Dim oPoints As Point() = oRegion.SeriesPoints
            sCoords = oPoints(0).X & "," & oPoints(0).Y

            For iPoint As Integer = 1 To oPoints.GetUpperBound(0)
                sCoords = sCoords & "," & oPoints(iPoint).X & "," & oPoints(iPoint).Y
            Next

            '---Now that we have the polygon, let's tell the web browser what 
            '   to do when our user clicks on a data point. Here we'll just display
            '   a javascript message, but anything is possible...
            Dim sArea As String = "<area " & _
                                        " shape=" & Chr(34) & "poly" & Chr(34) & _
                                        " coords=" & Chr(34) & sCoords & Chr(34)

            Dim lXValue As Long = oRegion.XValue
            Dim lYValue As Long = oRegion.YValue

            Dim oRow As DataRow = moData.Rows(lXValue)

            Select Case mlMode

                Case ViewMode.WeekView
                    sArea = sArea & " href=" & Chr(34) & "SQBlockingE.aspx?D=" & msDBID & "&W=" & HttpUtility.UrlEncode(oRow.Item("dtWeekDate")) & Chr(34)

                Case ViewMode.DayView
                    sArea = sArea & " href=" & Chr(34) & "SQBlockingE.aspx?D=" & msDBID & "&DT=" & HttpUtility.UrlEncode(oRow.Item("dtWeekDate")) & Chr(34)

                Case ViewMode.SessionDetail
                    sArea = sArea & " href=" & Chr(34) & "SQBlockingL.aspx?D=" & msDBID & _
                            "&SD=" & HttpUtility.UrlEncode(oRow.Item("dtWeekDate")) & _
                            "&ED=" & HttpUtility.UrlEncode(DateAdd(DateInterval.Hour, 1, CDate(oRow.Item("dtWeekDate")))) & Chr(34)

            End Select

            sArea = sArea & " alt=" & Chr(34) & oRegion.SeriesName & " value=" & oRegion.YValue & Chr(34) & ">" & _
                                        vbCrLf


            sImageMap = sImageMap & sArea
        Next

        Return sImageMap
    End Function

    Protected Function FormLabel() As String
        Dim oFactory As New SQLAutoDocLib.ScanHistory_Factory
        Dim sCurrentScan As String = ""
        Dim sLastScan As String = ""
        oFactory.GetTimeOfLastScan(moDatabase.ID, SQLAutoDocLib.ScanSchedule.Type.BlockScan, "SQLAutoDocLib.BlockLog", sCurrentScan, sLastScan)

        Dim sValue As String = ""

        Select Case mlMode
            Case ViewMode.WeekView
                sValue = "Blocking Trends for " & moDatabase.Name & " last scan completed " & sLastScan

            Case ViewMode.DayView
                sValue = "Blocking Trends for " & moDatabase.Name & " for w/b " & msWeek & " last scan completed " & sLastScan

            Case ViewMode.SessionDetail
                sValue = "Blocking Trends for " & moDatabase.Name & " for date " & msDay & " last scan completed " & sLastScan
        End Select

        If sCurrentScan > "" Then
            sValue = " scan in progress started " & sCurrentScan
        End If

        Return sValue
    End Function

    Protected Function ScanMode() As String
        Return "Size"
    End Function

    Protected Function EditMode() As String
        Return "Size"
    End Function

    Protected Function ServerID() As String
        Return moServer.ID
    End Function
End Class
